<script setup>
  import { onMounted, ref } from 'vue';
  import { showFailToast } from '../../../utils';
import { getTaskList } from '../../../apis/task';
    const page = ref(1)
    const size = ref(5)
    const status = ref(2)
    const deliveryList = ref([])
    const isLoading = ref(false)
    const isEmpty = ref(false)

    const getPickupList = async () => {
       // isLoading.value = true
       getTaskList(status.value,page.value,size.value,res => {
        if(res.code === 200){
          if(res.data.items == null || res.data.counts === '0'){
            isEmpty.value = true
            return
          }
          for(let i=0;i < res.data.items.length;i++){
            deliveryList.value.push(res.data.items[i])
          }
          isEmpty.value = deliveryList.value.length === 0
        }else{
          showFailToast('获取任务列表失败')
        }
        isLoading.value = false
      })
    }
    const refreshCallback = () => {
      console.log("下拉刷新");
      page.value=1;
      deliveryList.value=[];
      getPickupList()
    }
    onMounted(() => {
      console.log('在途onMounted');
      deliveryList.value=[];
      getPickupList()
    })
</script>
<template>
   <scroll-view scroll-y refresher-enabled class="scroll-view"
   @scrolltolower="++page;getPickupList()"
   @refresherrefresh="refreshCallback"
   :refresher-triggered="isLoading">
      <view class="scroll-view-wrapper">
        <view
          v-for="delivery in deliveryList"
          :key="delivery.id"
          class="task-card"
        >
          <navigator
            hover-class="none"
            :url="`/subpkg_task/detail/index?id=${delivery.id}`"
          >
            <view class="header">
              <text class="no">任务编号: {{ delivery.transportTaskId }}</text>

            </view>

            <view class="body">
              <view class="timeline">
                <view class="line">{{ delivery.startAddress }}</view>

                <view class="line">{{ delivery.endAddress }}</view>

              </view>

            </view>

          </navigator>
<view class="footer">
          <view class="label">到货时间</view>

          <view class="time">{{ delivery.planArrivalTime }}</view>

          <navigator
            v-if="delivery.status === 2"
            hover-class="none"
            :url="`/subpkg_task/delivery/index?id=${delivery.id}`"
            class="action"
          >
            交付
          </navigator>

          <navigator
            v-if="delivery.status === 4"
            hover-class="none"
            :url="`/subpkg_task/record/index?transportTaskId=${delivery.transportTaskId}&actualDepartureTime=${delivery.actualDepartureTime}`"
            class="action"
          >
            回车登记
          </navigator>

        </view>

      </view>

      <view v-if="isEmpty" @click="refreshCallback" class="task-blank">无在途货物</view>
      </view>

    </scroll-view>
</template>

<style lang="scss" scoped>
  @import './styles.scss';
</style>
